<template>
	<view class="">
		<view>
			<view class="set p30 bgw ">
				<image src="../../static/image/icon@2x.png" class="vm addressIcon" mode=""></image>
				<p class=" one">
					{{locationsTxt}}
				</p>
				<view @tap="removeInalids()" class="fr">
					删除
				</view>
			</view>
			<view class="contents">
				<view class="list p16">
					<view v-if="data != ''" v-for="(v,i) in data" :key="i" class="lists">
						<view @tap="shopSelect(i)" class="shop_title  p30">
							<view>
								<image v-show="v.act == false" class="select vm" src="../../static/image/nomal@2x.png"></image>
								<image v-show="v.act == true" class="select vm" src="../../static/image/select@2x.png"></image>
								<image @tap="GoUser(v)" class="logo vm" :src="v.avatarImg" mode=""></image>
								<p @tap="GoUser(v)" class="one"> {{v.username}}</p>
							</view>
						</view>
						<view class="main">
							<view v-for="(j,k) in v.productList" :key="k" class="box ">
								<view @tap="goodsSelect(i,k)" class="seleTab">
									<image v-show="j.act == false" class="selectShop vm" src="../../static/image/nomal@2x.png"></image>
									<image v-show="j.act == true" class="selectShop vm" src="../../static/image/select@2x.png"></image>
								</view>
								<!-- <view   class="seleTab"><image class="select" src="../../static/image/selected@2x.png"></image></view> -->
								<image @tap="GoGoodesDetails(j)" class="shop" :src="j.thumbnail" mode="aspectFit"></image>
								<view @tap="GoGoodesDetails(j)" class="txt">
									<p class="p1  one"> {{j.name}}</p>
									<p v-if="j.specifications == null " class=" one specifications"></p>
									<p v-if="j.specifications != null " class=" one specifications"> {{j.specifications}}</p>
									<view class="p3  one">
										<p>¥ <span>{{j.price}}</span></p>
										<span class="nums" @tap.stop>
											<image @tap="numReduce(i, k, j)" src="../../static/image/icon_jian@2x.png" mode=""></image>
											<input v-model="j.quantity" disabled type="number" />
											<image @tap="numAdd(i, k, j)" src="../../static/image/icon_jia@2x.png" mode=""></image>
										</span>
									</view>
								</view>
							</view>
						</view>
					</view>
					<!-- 失效的列表  -->
					<view v-if="invalid != '' " class="lists invalid bgw">
						<view class="title">
							<view class="fl">失效的商品</view>
							<view @tap="removeInalid()" class="fr">
								清空
								<image src="../../static/image/icon_delete@2x.png" class="vm" mode=""></image>
							</view>
						</view>
						<view v-for="(v,i) in invalid" :key="i" class="box ">
							<view class="seleTab removes_box">
								<image class="removes" src="../../static/image/icon_shixiao@2x.png"></image>
							</view>
							<image @tap.stop class="shop" :src="v.images" mode=""></image>
							<view @tap.stop class="txt">
								<p class="p1 text-df two"> {{v.name}} </p>
								<p class="p3 text-lg one">商品已下架</p>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view v-if="data != ''" class="bottoms p20" :style="{ bottom: bh }">
				<span @tap="all()">
					<image v-show="allState == false" class="vm" src="../../static/image/nomal@2x.png" mode=""></image>
					<image v-show="allState == true" class="vm" src="../../static/image/select@2x.png" mode=""></image>
					全选
				</span>
				<!-- <button style="margin-top: 16rpx;" @tap="goodsDel()" v-show="state == 1" class="fr cu-btn round line-red">删除</button> -->
				<view class="bottom fr">
					合计:
					<span class="c2">¥</span>
					<span class="c2"> {{ moneySum }}</span>
					<u-button @tap="GoCreatedOrder()" shape="circle" :ripple="true" type="success">
						去预定({{goodsSum}})
					</u-button>
				</view>
			</view>
		</view>
		<view v-if="data == '' &&  invalid == '' " class="  dataNone">
			<image src="../../static/image/shangpin@2x.png" mode=""></image>
			<p>暂无商品</p>
			<u-button @tap="GoIndex()" :ripple="true" shape="circle" type="success">
				去逛逛
			</u-button>
		</view>
		<models title="提示" msg="确定删除？" @success="delectGoods" ref="modeles"></models>
		<newlyBuild ref="newlyBuild"></newlyBuild>
	</view>
</template>
<script>
	import {
		cartList,
		cartupdate,
		cartDelete,
		orderCheckout
	} from '../../api/api.js'
	import models from '@/components/models/models.vue'
	import acc from '@/common/float.js'
	export default {
		components: {
			models
		},
		data() {
			return {
				allState: false,
				moneySum: 0,
				quantity: 0,
				pageNumber: 1,
				pageSize: 100,
				data: [],
				stops: 0,
				goodsSum: 0,
				bh: 0,
				selectArr: [],
				invalid: [],
				locationsTxt: '定位中',

			}
		},
		onLoad() {
			uni.onTabBarMidButtonTap(() => {
				this.$refs['newlyBuild'].show()
				// this.$navigateTo('../new/new')
			})
			this.bh = uni.getSystemInfoSync().windowBottom + 'px';
			let that = this
			let parameret = {
				latitude: this.$store.state.latitude,
				longitude: this.$store.state.longitude
			}
			plus.maps.Map.reverseGeocode(parameret, {}, function(event) {
				that.locationsTxt = event.address
			})
		},
		onShow() { 
			let locationsTxt = uni.getStorageSync('locationsTxt')
			if (locationsTxt != '') {
				this.locationsTxt = locationsTxt
			}
			console.log(this.$store.state.name);
			let token = uni.getStorageSync('token')
			if (token != '') {
				this.inits()
			}
		},
		mounted() {
			// let token = uni.getStorageSync('token')
			// if (token != '') {
			// 	this.inits()
			// }
		},
		onReachBottom() {
			if (this.stops == 0) {
				this.init()
			}
		},
		methods: {
			GoGoodesDetails(j) {
				this.$navigateTo('../GoodsDetails/GoodsDetails?id=' + j.productId)
			},
			async numReduce(i, k, j) {
				if (j.quantity > 1) {
					j.quantity--
				}
				let data = await cartupdate({
					cartId: j.cartId,
					quantity: j.quantity
				})
				if (this.selectArr.indexOf(j.cartId) != -1) {
					this.moneySum = acc.subtr(this.moneySum, j.price)
				}
			},
			async numAdd(i, k, j) {
				if (j.quantity < 98) {
					j.quantity++
				}
				let data = await cartupdate({
					cartId: j.cartId,
					quantity: j.quantity
				})
				if (this.selectArr.indexOf(j.cartId) != -1) {
					this.moneySum = acc.add(this.moneySum, j.price)
				}

			},
			shopSelect(i) {
				let actData = this.data[i].productList
				if (this.data[i].act == false) {
					this.data[i].act = true
					this.data[i].num = actData.length
					for (let k = 0; k < actData.length; k++) {
						if (this.selectArr.indexOf(actData[k].cartId) == -1) {
							this.selectArr.push(actData[k].cartId)
							actData[k].act = true
							this.goodsSum = this.goodsSum + 1
							let goodsPriceSum = acc.mul(actData[k].price, actData[k].quantity)
							this.moneySum = acc.add(this.moneySum, goodsPriceSum)
						}
					}
				} else {
					this.data[i].act = false
					this.data[i].num = 0
					for (let k = 0; k < actData.length; k++) {
						if (this.selectArr.indexOf(actData[k].cartId) != -1) {
							this.selectArr.splice(this.selectArr.indexOf(actData[k].cartId), 1)
							actData[k].act = false
							this.goodsSum = this.goodsSum - 1
							let goodsPriceSum = acc.mul(actData[k].price, actData[k].quantity)
							this.moneySum = acc.subtr(this.moneySum, goodsPriceSum)
						}
					}
				}

			},
			goodsSelect(i, k) {
				let cartId = this.data[i].productList[k].cartId
				if (this.selectArr.indexOf(cartId) == -1) {
					this.selectArr.push(cartId)
					this.data[i].productList[k].act = true
					this.data[i].num = this.data[i].num + 1
					this.goodsSum = this.goodsSum + 1
					let goodsPriceSum = acc.mul(this.data[i].productList[k].price, this.data[i].productList[k].quantity)
					this.moneySum = acc.add(this.moneySum, goodsPriceSum)
					if (this.data[i].num == this.data[i].sum) {
						this.data[i].act = true
					}
				} else {
					this.data[i].act = false
					this.data[i].productList[k].act = false
					this.data[i].num = this.data[i].num - 1
					this.goodsSum = this.goodsSum - 1
					let goodsPriceSum = acc.mul(this.data[i].productList[k].price, this.data[i].productList[k].quantity)
					this.moneySum = acc.subtr(this.moneySum, goodsPriceSum)


					this.selectArr.splice(this.selectArr.indexOf(cartId), 1)
				}
			},
			all() {
				this.selectArr = []
				this.moneySum = 0
				this.goodsSum = 0
				let data = this.data
				if (this.allState == 0) {
					this.allState = true
					for (let i = 0; i < data.length; i++) {
						data[i].act = true
						let actData = data[i].productList
						for (let k = 0; k < actData.length; k++) {
							this.selectArr.push(actData[k].cartId)
							this.goodsSum = this.goodsSum + 1
							this.data[i].num++
							let goodsPriceSum = acc.mul(this.data[i].productList[k].price, this.data[i].productList[k].quantity)
							this.moneySum = acc.add(this.moneySum, goodsPriceSum)
							actData[k].act = true
						}
					}
				} else {
					this.allState = false
					for (let i = 0; i < data.length; i++) {
						data[i].act = false
						let actData = data[i].productList
						this.data[i].num = 0
						for (let k = 0; k < actData.length; k++) {
							actData[k].act = false
						}
					}
				}

			},
			async removeInalid() {
				let ids = []
				for (let i = 0; i < this.invalid.length; i++) {
					ids.push(this.invalid[i].cartId)
				}
				ids = ids.join(',')
				let data = await cartDelete({
					cartIds: ids
				})
				this.inits()
			},
			async delectGoods() {
				let ids = this.selectArr.join(',')
				let data = await cartDelete({
					cartIds: ids
				})
				this.inits()
			},
			async removeInalids() {
				if (this.selectArr == '') {
					this.$u.toast(`请选择`);
				} else {
					this.$refs['modeles'].show()
				}

			},
			inits() {
				this.invalid = []
				this.selectArr = []
				this.data = []
				this.pageNumber = 1
				this.stops = 0
				this.goodsSum = 0
				this.moneySum = 0
				this.allState = 0
				this.init();
			},
			GoUser(v) {
				uni.navigateTo({
					url: '../users/users?id=' + v.userId
				})
			},
			async init() {
				let parameter = {
					pageNumber: this.pageNumber,
					pageSize: this.pageSize
				}
				let data = await cartList(parameter)
				data = data.data.list
				if (data.length != this.pageSize) {
					this.stops = 1
				}

				let arr = []
				if (data != '') {
					for (let i = 0; i < data.length; i++) {
						data[i].act = false
						data[i].sum = data[i].productList.length
						data[i].num = 0
						let actData = data[i].productList
						for (let k = 0; k < actData.length; k++) {
							actData[k].act = false
							if (actData[k].isMarketable == false) {
								arr.push(actData[k])
								if (actData.length == 1) {
									data.splice(i, 1);
									i = -1
								} else {
									data[i].productList.splice(k, 1);
									k = -1
								}

							}

						}
					}
				}
				if (arr != '') {
					this.invalid = [...this.invalid, ...arr]
				}
				console.log(this.invalid);
				this.pageNumber = this.pageNumber + 1
				this.data = [...this.data, ...data]
			},
			GoIndex() {
				uni.switchTab({
					url: '../index/index'
				})
			},
			GoCreatedOrder() {
				let data = this.data
				let state = 0
				for (let i = 0; i < data.length; i++) {
					if (data[i].act == true) {
						state++
					} 
				}
				
				if(this.selectArr == ''){
					this.$u.toast(`请选择`);
					return
				}
				
				if(state>1){
					this.$u.toast(`仅支持单店铺购买`);
				}else{
					 this.checkingOrder()
				} 
			},
			async checkingOrder() { 
				var selectArr = this.selectArr;
				var url = `../CreatedOrder/CreatedOrder?cartIds=${selectArr}`
				let data = await orderCheckout({
					cartIds:selectArr
				})
				if (data.code == 0) { 
					this.$common.navigateTo(url);
				}
			},
		}
	}
</script>

<style lang="less" scoped>
	@import './index';
</style>
